package sjtu.ist.warehouse4ws.model;

import java.util.ArrayList;
import java.util.List;



/**
 * CustEpcunits entity. @author MyEclipse Persistence Tools
 */

public class CustEpcunits extends Epcunits implements java.io.Serializable {


    // Fields    

	private String id;
    private String name;
    private String fileId;
    private String groupId;
    private String versionId;


	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}
     
     private List<CustEpcunit> epcUnits=new ArrayList<CustEpcunit>();
 	
 	public List<CustEpcunit> epcunitList(){
 		return epcUnits;
 	}

     public List<Epcunit> getEpcUnits() {
    	 List<Epcunit> list=new ArrayList<Epcunit>();
    	 for(CustEpcunit ce:epcUnits){
    		 list.add(ce);
    	 }
    	 return list;
     }

     public void setCustEpcUnits(List<CustEpcunit> custEpcUnits) {
 	    this.epcUnits = custEpcUnits;
     }

    // Constructors

    /** default constructor */
    public CustEpcunits() {
    }

    public CustEpcunits(Epcunits e) {
    	this.setFileId(e.getFileId());
    	this.setGroupId(e.getGroupId());
    	this.setId(e.getId());
    	this.setName(e.getName());
    	this.setUid(e.getUid());
    	this.setVersionId(e.getVersionId());
    }
	/** minimal constructor */
    public CustEpcunits(String id) {
        this.id = id;
    }
   
    // Property accessors

    public String getId() {
        return this.id;
    }
    
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
    	
        return this.name;
        
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public String getFileId() {
        return this.fileId;
    }
    
    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

   
    public CustEpcunit findSourceDataById(int id){
	    for (CustEpcunit sourceData: epcUnits){
		    if (sourceData.getId()==id)return sourceData;
	    }
	    return null;
    }
    public void bulidRelationship(int source, int target){
        CustEpcunit sourceData=this.findSourceDataById(source);
        if(sourceData!=null){
        	sourceData.getNext().add(target);      	
        }        

    }
    public void removeRelationship(CustEpcunit source, CustEpcunit target){
    	source.getNext().remove(source.getNext().indexOf(target.getId()));
    }
    public int findMaxId(){
    	int max=0;
    	for (CustEpcunit e:epcUnits){
    		if (max<e.getId())
    			max=e.getId();
    	}
    	return max;
    }
    
    public List<CustEpcunit> findNextFunctionForCust(int id,List<CustEpcunit> taskList){
	    CustEpcunit element=new CustEpcunit();
	    element=this.findSourceDataById(id);
	
		for (int i:element.getNext()){
			element=this.findSourceDataById(i);
			if (element.getType()=="function"
				|| element.getType()=="connector"
			|| element.getType()=="end") taskList.add(element);
			else findNextFunctionForCust(i,taskList);
		}
	
	    return taskList;
    }
    public List<CustEpcunit> findSource(CustEpcunit target){
    	List<CustEpcunit> sourceList=new ArrayList<CustEpcunit>();
    	for (CustEpcunit e:epcUnits){
    		for(int i:e.getNext()){
    			CustEpcunit next=this.findSourceDataById(i);
    			if (next==target) sourceList.add(e);
    			
    		}
    	}
    	return sourceList;
    }






}